package com.mall.cart.controller;

import com.common.constants.AuthConstant;
import com.common.vo.MemberVo;
import com.mall.cart.Interceptor.AuthInterceptor;
import com.mall.cart.service.ICartService;
import com.mall.cart.vo.Cart;
import com.mall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {
    @Autowired
    private ICartService iCartService;
    @GetMapping("/cart_list")
    public String queryCartList(HttpServletRequest request,Model model){
//        Object attribute = request.getSession().getAttribute(AuthConstant.AUTH_SESSION_REDIS);
//        MemberVo memberVo = AuthInterceptor.threadLocal.get();
        Cart carts = iCartService.getCartList();
        model.addAttribute("cart",carts);
        return "cartList";
    }

    /**
     * 加入购物车
     * @return
     */
    @GetMapping("/addCart")
    public String addCart(@RequestParam("skuId") String skuId, @RequestParam("num") Integer num, Model model) throws ExecutionException, InterruptedException {
        CartItem item = iCartService.addCart(skuId,num);
        model.addAttribute("item",item);
        return "success";
    }
    @GetMapping(value = "/{memberId}/getUserCartItems")
    @ResponseBody
    public List<CartItem> getUserCartItems(@PathVariable("memberId") long memberId){
       return iCartService.getUserCartItems();
    }
}
